<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>购物车小案例</title>
    <script src="../../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>
            单价: {{dollar}}元
        </p>
        <p>
            数量: {{count }}个
        </p>
       
        <p>
            总价: {{sub|fixed}}元
        </p>
        <p>
            <button v-on:click="add">增加</button>
            <button v-on:click="jian">减少</button>
        </p>
    </div>
    <script>
        var v_app = new Vue({
            el: "#app",
            data: {
                dollar: 12.51322,
                count: 2
            },
            methods: {
                add() {
                    this.count++;
                },
                jian() {
                    if (this.count > 0) {
                        this.count--;
                    }
                }
            },
            computed: {
                sub() {
                    return Number(this.dollar) * Number(this.count)
                }
            },
            filters: {
                fixed(str) {
                    return str.toFixed(2);
                }
            }
        });
    </script>
</body>

</html>